﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Fluid
{
    public class Fluid:IFluid
    {
   

        public int GetFluidCount(int userId, DateTime clientDateTime, Model.NutriLeafDBEntities context)
        {
            return context.FluidDiaries.Where(p => p.UserID == userId).Count();
            
        }

        public void RemoveFluid(int userId, DateTime clientDateTime, Model.NutriLeafDBEntities context)
        {
            Model.FluidDiary []fluid = context.FluidDiaries.Where(p => p.ClientDateTimeStamp == clientDateTime && p.UserID == userId).ToArray();
            foreach (Model.FluidDiary fl in fluid) {
                context.FluidDiaries.DeleteObject(fl);
               
            }
            context.SaveChanges();
        }

        public void RemoveRecentFluid(int userId, Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.FluidDiaries.Where(p => p.UserID == userId).First());
            context.SaveChanges();
        }

        public List<Model.FluidDiary> GetFluids(int userId, DateTime clientDateTime, Model.NutriLeafDBEntities context)
        {
            return context.FluidDiaries.Where(p => p.UserID==userId && p.ClientDateTimeStamp == clientDateTime).ToList();
        }

        public List<Model.FluidDiary> GetFluids(int userId, DateTime clientDateTimeStart, DateTime clientDateTimeEnd, Model.NutriLeafDBEntities context)
        {
            return context.FluidDiaries.Where(p => p.UserID == userId && p.ClientDateTimeStamp >= clientDateTimeStart && p.ClientDateTimeStamp < clientDateTimeEnd).ToList();
        }

        public void DrinkFluid(Model.FluidDiary dairy, int userId, DateTime clientdateTime, Model.NutriLeafDBEntities context)
        {
            context.AddToFluidDiaries(dairy);
            context.SaveChanges();
        }
    }
}